﻿using MiniExcelLibs;
using PanhuaWin.通用;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace PanhuaWin.仓库.采购入库
{
    public partial class Form原料卷入库 : UIPage
    {
        /// <summary>
        /// 临时表的guid
        /// </summary>
        static string tguid = "";
        /// <summary>
        /// 选中的合同行
        /// </summary>
        T_VIEW_CON t_view_con = new T_VIEW_CON();
        string 工厂编号1;
        string 工厂编号2 = pub.全局工厂编号;
        public Form原料卷入库()
        {
            InitializeComponent();
        }



        private void Form原料卷入库_Load(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Maximized;
            this.uiDatePicker开始日期.Value = DateTime.Today.AddMonths(-1);
            this.uiDatePicker结束日期.Value = DateTime.Today;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop2);
            pub.ListContractType(this.uiComboBox合同类型);
            pub.ListAppUserName(this.uiComboBox申请人);
            pub.ListGoods(this.uiComboBoxGoods);
            pub.ListSteel(this.uiComboBox材质1);
            pub.ListSteel(this.uiComboBox材质2);
            this.uiTextBox虚拟值1.Text = "C";
            this.tabPage2.Select();
            //pub.ListWareHouse(this.uiComboBox仓库编号1);

        }

        private void uiButton合同查询_Click(object sender, EventArgs e)
        {
            List原料采购合同();
        }

        private void List原料采购合同()
        {
            string sql;
            //人员权限问题
            //
            sql = @"select ROW_NUMBER() OVER(  ORDER BY guid) 行号,  guid, CID, T_APP_DPT_CID, T_APP_DPT_NAME, T_APP_USER_CODE, T_APP_USER_NAME, 
                T_APP_USER_TEL, T_APP_USER_DATE, T_EXECUTOR_NAME, T_EXECUTOR_CODE, EXP40, T_DRAFT_NAME, 
                T_RELATED_NAME, T_RELATED_CID, T_CONTRACT_CODE, T_CONTRACT_NAME, T_CONTRACT_TYPE_NAME, 
                T_CONTRACT_TYPE_CID, T_CONTRACT_MONEY, T_MONEY_UNIT, T_MONEY_CAPITAILZATION, T_RATE, 
                T_RATE_BEFOR_MONEY, T_TEAN_TYPE, T_START_DATE, T_END_DATE, T_TEAN, T_TEAN_UNIT, T_PAY_WAY, 
                T_PAY_WAY_CID, T_PROJECT_NAME, T_PROJECT_CID, T_PROJECT_USER_NAME, T_PROJECT_USER_CODE, 
                T_REMARK, T_SUBJECT_NAME, T_SUBJECT_CID, T_SEAL, T_SEAL_PATH, T_GOOD_CID, T_ORDER, T_GOOD_CODE, 
                T_GOOD_NAME, T_GOOD_MODEL, T_GOOD_PRICE, T_GOOD_NUM, T_GOOD_UNIT, T_GOOD_MONEY, T_GOOD_TIME, 
                T_GOOD_ADDRESS, T_GOOD_REMARK, T_GOOD_TRANSACTION_NUM, T_GOOD_EXP1, T_GOOD_EXP2, 
                T_GOOD_EXP3, T_GOOD_EXP4, T_RELATIVE_NAME
FROM      T_VIEW_CON ";
            sql += " where T_APP_USER_DATE >='" + this.uiDatePicker开始日期.Value + "' and T_APP_USER_DATE<'" + this.uiDatePicker结束日期.Value.AddDays(-1) + "'";
            sql += " and T_SUBJECT_NAME='" + this.uiComboBoxWorkShop.Text + "'";
            sql += " and T_CONTRACT_TYPE_NAME='" + this.uiComboBox合同类型.Text + "'";
            sql += " order by T_CONTRACT_CODE,T_ORDER";
            if (uiTextBoxGood.Text != "")
            {
                sql += " and T_GOOD_NAME like '%" + this.uiTextBoxGood.Text.Trim().Replace("--", "") + "%'";

            }



            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            this.uiDataGridView采购合同.AutoGenerateColumns = false;
            this.uiDataGridView采购合同.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView采购合同.DataSource = tb;
            this.uiDataGridView采购合同.Refresh();

        }

        private void uiComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void uiTextBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiLabel9_Click(object sender, EventArgs e)
        {

        }

        private void uiSplitContainer4_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void uiSplitContainer3_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void Form原料卷入库_Resize(object sender, EventArgs e)
        {
            this.uiTabControl1.Left = 0;
            this.uiTabControl1.Top = 0;
            this.uiTabControl1.Width = this.Width - 10;
            this.uiTabControl1.Height = this.Height - 10;
        }

        private void uiDataGridView采购合同_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string cid = this.uiDataGridView采购合同.Rows[e.RowIndex].Cells["C合同CID"].Value.ToString();
                t_view_con = pub.GetT_VIEW_CON(cid);
                if (t_view_con != null)
                {
                    this.uiTextBox存货编号1.Text = t_view_con.T_GOOD_CODE.ToString();
                    this.uiTextBox选中的合同号1.Text = t_view_con.T_CONTRACT_CODE.ToString();
                    this.uiTextBox选中的合同行1.Text = t_view_con.T_ORDER.ToString();
                    this.uiTextBox存货名称1.Text = t_view_con.T_GOOD_NAME.ToString();
                    this.uiTextBox规格型号1.Text = t_view_con.T_GOOD_MODEL.ToString();
                    this.uiTextBox计量单位1.Text = t_view_con.T_GOOD_UNIT.ToString();
                }
                else
                {
                    this.uiTextBox存货编号1.Text = "";
                    this.uiTextBox选中的合同号1.Text = "";
                    this.uiTextBox选中的合同行1.Text = "";
                    this.uiTextBox存货名称1.Text = "";
                    this.uiTextBox规格型号1.Text = "";
                    this.uiTextBox计量单位1.Text = "";

                }
            }



        }

        private void uiSplitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void uiSplitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void uiSplitContainer3_SplitterMoved_1(object sender, SplitterEventArgs e)
        {

        }

        private void uiSplitContainer8_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void uiLabel7_Click(object sender, EventArgs e)
        {

        }

        private void uiButton新建入库单1_Click(object sender, EventArgs e)
        {
            ///清空界面所选
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
                ClearAll2();

            }
            else
            {
                DeleteTemp(tguid);
                tguid = Guid.NewGuid().ToString();
                ClearAll2();
            }

        }

        private void ClearAll2()
        {
            this.uiTextBox卷重量2.Text = "";
            this.uiTextBox原料卷号2.Text = "";
            this.uiTextBox存货编号2.Text = "";
            this.uiTextBox存货名称2.Text = "";
            this.uiTextBox规格型号2.Text = "";
            this.uiTextBox计量单位2.Text = "";
            this.uiTextBox产地2.Text = "";
            this.uiTextBox厚度2.Text = "";
            this.uiTextBox宽度2.Text = "";
            uiTextBox手填合同号.Text = "";
            this.uiDataGridView无合同原料卷.DataSource = null;
            this.uiDataGridView无合同原料卷.Refresh();
        }

        private void DeleteTemp(string tguid)
        {
            string sql;
            sql = "delete Temp_原料卷入库 where guid ='" + tguid + "'  or mac='" + pub.getMac() + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除原料入库的临时表");

        }

        private void ClearAll1()
        {
            this.uiTextBox卷重量1.Text = "";
            this.uiTextBox原料卷号1.Text = "";
            this.uiTextBox存货编号1.Text = "";
            this.uiTextBox存货名称1.Text = "";
            this.uiTextBox规格型号1.Text = "";
            this.uiTextBox计量单位1.Text = "";
            this.uiTextBox选中的合同号1.Text = "";
            this.uiTextBox选中的合同行1.Text = "";

            this.uiDataGridView合同原料卷.DataSource = null;
            this.uiDataGridView合同原料卷.Refresh();


        }

        private void uiButton添加1_Click(object sender, EventArgs e)
        {
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
            }
            string sql;
            int 序号;
            string 厚度;
            string 宽度;
            string 虚拟值;
            string 合同号 = this.uiTextBox选中的合同号1.Text.Trim().Replace("--", "");
            string 合同行号 = this.uiTextBox选中的合同行1.Text.Trim().Replace("--", "");
            string 存货编号 = this.uiTextBox存货编号1.Text.Trim().Replace("--", "");
            string 存货名称 = this.uiTextBox存货名称1.Text.Trim().Replace("--", "");
            string 规格型号 = this.uiTextBox规格型号1.Text.Trim().Replace("--", "");
            try
            {
                string[] gg = 规格型号.Split("*");
                厚度 = gg[0];
                宽度 = gg[1];
                虚拟值 = gg[2];
            }
            catch
            {
                厚度 = this.uiTextBox厚度1.Text;
                宽度 = this.uiTextBox宽度1.Text;
                虚拟值 = this.uiTextBox虚拟值1.Text;
            }



            string 计量单位 = this.uiTextBox计量单位1.Text.Trim().Replace("--", "");
            string 原料卷号 = this.uiTextBox原料卷号1.Text.Trim().Replace("--", "");
            string 新卷号 = this.uiTextBox新卷号1.Text.Trim().Replace("--", ""); ;
            decimal 卷重量 = pub.Todecimal(this.uiTextBox卷重量1.Text.Trim().Replace("--", ""));
            string 入库仓库 = this.uiComboBox仓库编号1.SelectedValue.ToString();
            string 入库库位 = this.uiComboBox入库库位1.SelectedValue.ToString();
            string 材质 = this.uiComboBox材质1.SelectedText.ToString();
            string 产地 = this.uiTextBox产地1.Text.ToString().Trim();

            #region 检查
            if (合同号 == "")
            {
                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (合同行号 == "")
            {
                MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (存货编号 == "")
            {
                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (原料卷号 == "")
            {
                MessageBox.Show("原料卷号未填写!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (产地 == "")
            {
                MessageBox.Show("产地未填写!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (卷重量 <= 0)
            {
                MessageBox.Show("卷重量有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库仓库 == "")
            {
                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库库位 == "")
            {
                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (产地 == "")
            {
                MessageBox.Show("产地有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (材质 == "")
            {
                MessageBox.Show("材质有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            ///检查原料卷号是否存在了
            if (pub.IsExistVolume(原料卷号))
            {
                MessageBox.Show("原料卷号已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                return;

            }

            if (IsExistTempVolume(原料卷号))
            {
                MessageBox.Show("原料卷号这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (!pub.CheckSize(规格型号))
            {
                MessageBox.Show("规格型号的表示是不正确的,请正确填写!", "提示", MessageBoxButtons.OK);
                return;
            }
            序号 = this.uiDataGridView合同原料卷.Rows.Count + 1;
            if (t_view_con.T_CONTRACT_CODE == "")
            {
                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            #endregion

            ///塞入临时表并显示
            sql = "insert into Temp_原料卷入库 (guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,新卷号,入库仓库,入库库位,产地,材质, 卷重量,MAC,时间,T_GOOD_NUM,T_GOOD_PRICE,T_GOOD_MONEY,CID ,Thick, Width, Material,Virtual  )";
            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "', '" + 计量单位 + "', '" + 原料卷号 + "','" + 新卷号 + "','" + 入库仓库 + "','" + 入库库位 + "','" + 产地 + "','" + 材质 + "', '" + 卷重量 + "',";
            sql += "'" + pub.getMac() + "',getdate(),'" + t_view_con.T_GOOD_NUM + "','" + t_view_con.T_GOOD_PRICE + "','" + t_view_con.T_GOOD_MONEY + "','" + t_view_con.CID + "','" + 厚度 + "','" + 宽度 + "','" + 材质 + "','" + 虚拟值 + "' )";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入原料入库临时表") > 0)
            {
                //更新序号
                sql = "select guid, 序号   FROM      Temp_原料卷入库 where guid='" + tguid + "'  order by 序号 ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb != null)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        sql = "update Temp_原料卷入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "更新原料入库临时表");
                    }
                }
                ListTemp_原料卷入库(tguid, this.uiDataGridView合同原料卷);
            }
            else
            {
                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                return;

            }





        }

        private void ListTemp_原料卷入库(string guid, UIDataGridView uidatagridview)
        {
            string sql;
            sql = "select  guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,新卷号, 入库仓库,入库库位,产地,材质,卷重量 ";
            sql += " FROM Temp_原料卷入库  where guid='" + guid + "' order by 序号 ";


            DataTable tb1 = pub.GetDataTable(pub.ConnectionString, sql);
            uidatagridview.AutoGenerateColumns = false;
            uidatagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uidatagridview.DataSource = tb1;
            uidatagridview.Refresh();
        }

        /// <summary>
        /// 是否临时表已经有了这个卷号
        /// </summary>
        /// <param name="tguid"></param>
        /// <returns></returns>
        private bool IsExistTempVolume(string 原料卷号)
        {
            string sql;
            sql = "select * from Temp_原料卷入库 where  原料卷号='" + 原料卷号 + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void uiComboBox仓库编号1_Click(object sender, EventArgs e)
        {

        }

        private void uiTextBoxGood_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton保存1_Click(object sender, EventArgs e)
        {
            ///将临时表塞入入库单
            ///
            string sql;

            ///先验证是否重复了
            sql = "select guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,新卷号, 卷重量    FROM Temp_原料卷入库 where guid='" + tguid + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (pub.IsExistVolume(tb.Rows[i]["原料卷号"].ToString()))
                    {

                        MessageBox.Show("要保存的卷【" + tb.Rows[i]["原料卷号"].ToString() + "】已经有保存过了!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            string stockInType = "原料卷入库";

            string workShopCode = 工厂编号1;
            string docCode = pub.GetDocCode("原料入库", workShopCode);
            if (this.uiTextBox部门1.Text == "")
            {
                MessageBox.Show("部门未选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            string departCode = pub.GetDepartByCode(this.uiTextBox部门1.Text, 工厂编号1).DepartCode;
            string guid = Guid.NewGuid().ToString();
            //string warehouseCode = this.uiComboBox仓库编号1.SelectedValue.ToString();//仓库编号
            //string storageLocation = this.uiComboBox入库库位1.SelectedValue.ToString();//库位编号
            // string StorageLocation = this.uiComboBox入库库位1.SelectedValue.ToString();
            ///塞入原料主表
            if (docCode == "")
            {
                MessageBox.Show("未获得单号,请联系系统管理员!", "提示", MessageBoxButtons.OK);
                return;
            }

            sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','" + stockInType + "','" + workShopCode + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
            sql += @" insert into Bus_StockInVolume(guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, T_CONTRACT_CODE, 
                T_ORDER, T_GOOD_UNIT, T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, VolumeCode,VolumeCodeNew, InventoryCode, Size, 
                Thick, Width, Material,Virtual, Weight, CID ,Address
)";
            sql += " select  '" + guid + "',newid(),'" + docCode + "',  序号,'" + workShopCode + "', 入库仓库,入库库位,合同号,合同行号,计量单位,isnull(T_GOOD_NUM,0), isnull(T_GOOD_PRICE,0), isnull(T_GOOD_MONEY,0),原料卷号,新卷号,存货编号,规格型号,Thick, Width, Material,Virtual,卷重量,CID,产地 ";
            sql += " from  Temp_原料卷入库 where guid='" + tguid + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "保存原料入库单据");
            ///检查是否成功
            sql = "select * from Bus_StockInV where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sql);
            if (tbr.Rows.Count > 0)
            {
                sql = "select * from Bus_StockInVolume where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tbb.Rows.Count > 0)
                {
                    MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);
                    sql = "delete  Temp_原料卷入库 where guid='" + tguid + "' or mac='" + pub.getMac() + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "保存成功后删除原料入库临时表");
                    tguid = "";
                    ClearAll1();

                    return;
                }
                else
                {
                    sql = "delete  Bus_StockInV  where guid='" + guid + "'";

                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
            else
            {
                sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return;
            }



        }

        private void uiComboBoxWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                pub.ListWareHouse(this.uiComboBox仓库编号1, tb.Rows[0]["guid"].ToString());
                if (this.uiComboBox仓库编号1.SelectedValue.ToString() != "")
                {
                    Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
                }

            }

        }

        private void uiComboBox仓库编号1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());


        }

        private void Do库位刷新1(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox入库库位1.DataSource = null;
                uiComboBox入库库位1.Refresh();
                pub.ListLocation(this.uiComboBox入库库位1, 工厂编号1, this.uiTextBox库位搜索1.Text, 仓库编号);
            }
        }
        private void Do库位刷新2(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号2 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox入库库位2.DataSource = null;
                uiComboBox入库库位2.Refresh();
                pub.ListLocation(this.uiComboBox入库库位2, 工厂编号2, this.uiTextBox库位搜索2.Text, 仓库编号);
            }
        }

        private void uiTextBox部门1_DoubleClick(object sender, EventArgs e)
        {


        }

        private void uiButton选择部门_Click(object sender, EventArgs e)
        {
            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门1.Text = form部门档案.部门编号;
                this.uiTextBox部门名称1.Text = form部门档案.部门名称;
            }
        }

        private void uiDataGridView合同原料卷_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.uiDataGridView合同原料卷.Columns[e.ColumnIndex].Name == "D删除")
            {
                string sql;
                int 序号 = pub.ToInt(this.uiDataGridView合同原料卷.Rows[e.RowIndex].Cells["D序号"].Value.ToString());
                sql = " delete Temp_原料卷入库 where (guid='" + tguid + "' and 序号='" + 序号 + "') or (mac ='" + pub.getMac() + "' and guid<>'" + tguid + "')";
                if (pub.ExecuteSql(pub.ConnectionString, sql, "原料入库临时表删除行") >= 0)
                {
                    //更新序号
                    sql = "select guid, 序号   FROM      Temp_原料卷入库 where guid='" + tguid + "'  order by 序号 ";
                    DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                    if (tb != null)
                    {
                        for (int i = 0; i < tb.Rows.Count; i++)
                        {
                            sql = "update Temp_原料卷入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                            pub.ExecuteSql(pub.ConnectionString, sql, "更新原料入库临时表的行号");
                        }
                    }
                    ListTemp_原料卷入库(tguid, this.uiDataGridView合同原料卷);
                }
                else
                {
                    MessageBox.Show("删除出错了,请检查");
                    return;
                }

            }
        }

        private void uiTextBox厚度1_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void SetSize()
        {
            this.uiTextBox规格型号1.Text = pub.Todecimal(this.uiTextBox厚度1.Text, 3) + "*" + pub.ToInt(this.uiTextBox宽度1.Text) + "*" + this.uiTextBox虚拟值1.Text;
        }

        private void uiTextBox宽度1_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void uiTextBox虚拟值1_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void uiButton选择部门2_Click(object sender, EventArgs e)
        {
            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop2.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门2.Text = form部门档案.部门编号;
                this.uiTextBox部门名称2.Text = form部门档案.部门名称;
            }
        }

        private void uiButton选择存货_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();

            form存货档案.存货分类 = "热轧";

            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号2.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称2.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号2.Text = form存货档案.存货档案.T_GOOD_MODEL;
                this.uiTextBox计量单位2.Text = form存货档案.存货档案.T_GOOD_UNIT;
                try
                {
                    string[] gg = uiTextBox规格型号2.Text.Split("*");
                    this.uiTextBox厚度2.Text = gg[0];
                    this.uiTextBox宽度2.Text = gg[1];
                    this.uiTextBox虚拟值2.Text = gg[2];
                }
                catch
                {
                    MessageBox.Show("规格不符合格式!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
        }

        private void uiTextBox库位搜索1_TextChanged(object sender, EventArgs e)
        {
            Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
        }

        private void uiTextBox库位搜索2_TextChanged(object sender, EventArgs e)
        {
            Do库位刷新2(this.uiComboBox仓库编号2.SelectedValue.ToString());
        }

        private void uiComboBoxWorkShop2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号2 = this.uiComboBoxWorkShop2.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号2 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                pub.ListWareHouse(this.uiComboBox仓库编号2, tb.Rows[0]["guid"].ToString());
                Do库位刷新2(this.uiComboBox仓库编号2.SelectedValue.ToString());
            }
        }

        private void uiTextBox供应商条件_TextChanged(object sender, EventArgs e)
        {
            pub.ListSupplier(this.uiComboBox供应商2, this.uiTextBox供应商条件2.Text);
        }

        private void uiButton添加2_Click(object sender, EventArgs e)
        {
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
            }
            string sql;
            int 序号;
            string 合同号 = this.uiTextBox手填合同号.Text.Trim().Replace("--", "");
            string 合同行号 = this.numericUpDown合同行号.Value.ToString();
            string 存货编号 = this.uiTextBox存货编号2.Text.Trim().Replace("--", "");
            string 存货名称 = this.uiTextBox存货名称2.Text.Trim().Replace("--", "");
            string 规格型号 = this.uiTextBox规格型号2.Text.Trim().Replace("--", "");
            string 厚度 = this.uiTextBox厚度2.Text;
            string 宽度 = this.uiTextBox宽度2.Text;
            string 虚拟值 = this.uiTextBox虚拟值2.Text;
            string 计量单位 = this.uiTextBox计量单位2.Text.Trim().Replace("--", "");
            string 原料卷号 = this.uiTextBox原料卷号2.Text.Trim().Replace("--", "");
            string 新卷号 = this.uiTextBox新卷号2.Text.Trim().Replace("--", "");

            decimal 卷重量 = pub.Todecimal(this.uiTextBox卷重量2.Text.Trim().Replace("--", ""));
            string 入库仓库 = "";
            if (uiComboBox仓库编号2.SelectedText != "")
            {
                入库仓库 = this.uiComboBox仓库编号2.SelectedValue.ToString();
            }
            else
            {
                MessageBox.Show("仓库编号未选!", "提示", MessageBoxButtons.OK);
                return;
            }
            string 入库库位 = "";
            if (uiComboBox入库库位2.SelectedText != "")
            {
                入库库位 = this.uiComboBox入库库位2.SelectedValue.ToString();
            }


            string 材质 = this.uiComboBox材质2.SelectedText.ToString();
            string 产地 = this.uiTextBox产地2.Text;

            #region 检查
            //if (合同号 == "")
            //{
            //    MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            //if (合同行号 == "")
            //{
            //    MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            if (存货编号 == "")
            {
                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (原料卷号 == "")
            {
                MessageBox.Show("原料卷号未填写!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (卷重量 <= 0)
            {
                MessageBox.Show("卷重量有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库仓库 == "")
            {
                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库库位 == "")
            {
                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            ///检查原料卷号是否存在了
            if (pub.IsExistVolume(原料卷号))
            {
                MessageBox.Show("原料卷号已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                return;

            }

            if (IsExistTempVolume(原料卷号)) ///包含所有其他人员都可能用到的
            {
                MessageBox.Show("原料卷号这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (!pub.CheckSize(规格型号))
            {
                MessageBox.Show("规格型号的表示是不正确的,请正确填写!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (合同号 == "")
            {
                合同行号 = "0";
            }
            else
            {
                if (合同行号 == "0")
                {
                    合同行号 = "1";
                }
            }

            序号 = this.uiDataGridView无合同原料卷.Rows.Count + 1;
            //if (t_view_con.T_CONTRACT_CODE == "")
            //{
            //    MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            #endregion

            ///塞入临时表并显示
            sql = "insert into Temp_原料卷入库 (guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,新卷号,入库仓库,入库库位,产地,材质, 卷重量,MAC,时间,T_GOOD_NUM,T_GOOD_PRICE,T_GOOD_MONEY,CID ,Thick, Width, Material,Virtual  )";
            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "', '" + 计量单位 + "', '" + 原料卷号 + "','" + 新卷号 + "','" + 入库仓库 + "','" + 入库库位 + "','" + 产地 + "','" + 材质 + "', '" + 卷重量 + "',";
            sql += "'" + pub.getMac() + "',getdate(),'" + t_view_con.T_GOOD_NUM + "','" + t_view_con.T_GOOD_PRICE + "','" + t_view_con.T_GOOD_MONEY + "','" + t_view_con.CID + "','" + 厚度 + "','" + 宽度 + "','" + 材质 + "','" + 虚拟值 + "' )";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入原料入库临时表") > 0)
            {
                //更新序号
                sql = "select guid, 序号   FROM      Temp_原料卷入库 where guid='" + tguid + "'  order by 序号 ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb != null)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        sql = "update Temp_原料卷入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "更新原料入库临时表");
                    }
                }
                ListTemp_原料卷入库(tguid, this.uiDataGridView无合同原料卷);
            }
            else
            {
                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                return;

            }
        }

        private void uiButton选择存货1_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();
            form存货档案.存货分类 = "热轧";
            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号1.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称1.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号1.Text = form存货档案.存货档案.T_GOOD_MODEL;
                this.uiTextBox计量单位1.Text = form存货档案.存货档案.T_GOOD_UNIT;
                try
                {
                    string[] gg = uiTextBox规格型号1.Text.Split("*");
                    this.uiTextBox厚度1.Text = gg[0];
                    this.uiTextBox宽度1.Text = gg[1];
                    this.uiTextBox虚拟值1.Text = gg[2];
                }
                catch
                {
                    MessageBox.Show("规格不符合格式!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
        }

        private void uiButton导出模板1_Click(object sender, EventArgs e)
        {

            string path;
            this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            string sql;
            sql = " select   序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号,产地,材质, 原料卷号,新卷号, 卷重量 ,计量单位 ";
            sql += " FROM Temp_原料卷入库 where  1=2";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }




        }

        private void uiButton导入1_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                //openFileDialog1.InitialDirectory = Application.ExecutablePath;
                //openFileDialog1.Filter = "Excel文件|*.xlsx";
                //openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                this.openFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX"; 
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    if (table.Rows.Count > 0)
                    {
                        if (tguid == "")
                        {
                            tguid = Guid.NewGuid().ToString();
                        }
                        for (int i = 0; i < table.Rows.Count; i++)
                        {

                            string sql;
                            int 序号;
                            string 厚度;
                            string 宽度;
                            string 虚拟值;
                            string 合同号;
                            if (table.Rows[i]["合同行号"].ToString() != "")
                            {
                                合同号 = table.Rows[i]["合同号"].ToString();
                            }
                            else
                            {
                                合同号 = this.uiTextBox选中的合同号1.Text;
                            }
                            string 合同行号;
                            if (table.Rows[i]["合同行号"].ToString() != "")
                            {
                                合同行号 = table.Rows[i]["合同行号"].ToString();
                            }
                            else
                            {
                                合同行号 = this.uiTextBox选中的合同行1.Text;
                            }

                            string 存货编号 = table.Rows[i]["存货编号"].ToString();
                            string 存货名称 = table.Rows[i]["存货名称"].ToString();
                            string 规格型号 = table.Rows[i]["规格型号"].ToString();
                            try
                            {
                                string[] gg = 规格型号.Split("*");

                                厚度 = gg[0];
                                宽度 = gg[1];
                                虚拟值 = gg[2];
                            }
                            catch
                            {
                                MessageBox.Show("规格不符合格式!", "提示", MessageBoxButtons.OK);
                                return;
                            }


                            string 计量单位 = table.Rows[i]["计量单位"].ToString();
                            string 原料卷号 = table.Rows[i]["原料卷号"].ToString();
                            string 新卷号 = table.Rows[i]["新卷号"].ToString();
                            decimal 卷重量 = pub.Todecimal(table.Rows[i]["卷重量"].ToString());

                            string 入库仓库 = pub.getWareHouse(table.Rows[i]["入库仓库"].ToString());

                            string 入库库位 = pub.getLocation(table.Rows[i]["入库库位"].ToString());

                            string 材质 = table.Rows[i]["材质"].ToString();
                            string 产地 = table.Rows[i]["产地"].ToString();

                            #region 检查
                            if (合同号 == "")
                            {
                                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (合同行号 == "")
                            {
                                MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (存货编号 == "")
                            {
                                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (原料卷号 == "")
                            {
                                MessageBox.Show("原料卷号未填写!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (卷重量 <= 0)
                            {
                                MessageBox.Show("卷重量有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (入库仓库 == "")
                            {
                                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (入库库位 == "")
                            {
                                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (材质 == "")
                            {
                                MessageBox.Show("材质有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (产地 == "")
                            {
                                MessageBox.Show("产地有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            ///检查原料卷号是否存在了
                            if (pub.IsExistVolume(原料卷号))
                            {
                                MessageBox.Show("原料卷号已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;

                            }
                            ///检查原料卷号是否存在了
                            if (新卷号 != "")
                            {
                                if (pub.IsExistVolumeNew(新卷号))
                                {
                                    MessageBox.Show("新卷号已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                                    pub.Delete原料入库临时数据();
                                    return;

                                }

                            }


                            if (IsExistTempVolume(原料卷号))
                            {
                                MessageBox.Show("原料卷号这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (!pub.CheckSize(规格型号))
                            {
                                MessageBox.Show("规格型号的表示是不正确的,请正确填写!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            序号 = i + 1;
                            t_view_con = pub.GetT_VIEW_CON(合同号, 合同行号);
                            if (t_view_con == null)
                            {
                                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            #endregion

                            ///塞入临时表并显示
                            sql = "insert into Temp_原料卷入库 (guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,入库仓库,入库库位,产地,材质, 卷重量,MAC,时间,T_GOOD_NUM,T_GOOD_PRICE,T_GOOD_MONEY,CID ,Thick, Width, Material,Virtual  )";
                            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "', '" + 计量单位 + "', '" + 原料卷号 + "','" + 入库仓库 + "','" + 入库库位 + "','" + 产地 + "','" + 材质 + "', '" + 卷重量 + "',";
                            sql += "'" + pub.getMac() + "',getdate(),'" + t_view_con.T_GOOD_NUM + "','" + t_view_con.T_GOOD_PRICE + "','" + t_view_con.T_GOOD_MONEY + "','" + t_view_con.CID + "','" + 厚度 + "','" + 宽度 + "','" + 材质 + "','" + 虚拟值 + "' )";
                            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入原料入库临时表") > 0)
                            {


                            }
                            else
                            {
                                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;

                            }
                        }
                        ListTemp_原料卷入库(tguid, this.uiDataGridView合同原料卷);
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
                return;
            }

        }

        private void uiTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiTabControl1.SelectedIndex == 1)
            {
                pub.ListSupplier(this.uiComboBox供应商2, this.uiTextBox供应商条件2.Text);

            }
        }

        private void uiComboBox供应商2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void uiComboBox仓库编号2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Do库位刷新2(this.uiComboBox仓库编号2.SelectedValue.ToString());
        }

        private void uiButton导出模板2_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            string sql;
            sql = " select '' 序号	,''品名,''规格,''批号,	''重量,''产地,''交接单号,''入库单号,''入库日期,''原批号,''材质,''合同号,''	库位,''备注,''入库仓库";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiButton导入2_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                this.openFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX"; 
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    if (table.Rows.Count > 0)
                    {
                        if (tguid == "")
                        {
                            tguid = Guid.NewGuid().ToString();
                        }
                        for (int i = 0; i < table.Rows.Count; i++)
                        {

                            string sql;
                            string 存货编号 = "";
                            string 存货名称 = "";
                            string 序号 = (i + 1).ToString();
                            string 品名 = table.Rows[i]["品名"].ToString();
                            string 规格型号 = table.Rows[i]["规格"].ToString();
                            string 新卷号 = table.Rows[i]["批号"].ToString();
                            string 合同号 = table.Rows[i]["合同号"].ToString();
                            decimal 卷重量 = 0;
                            if (decimal.TryParse(table.Rows[i]["重量"].ToString(), out decimal a))
                            {
                                卷重量 = a;
                            }
                            else
                            {
                                MessageBox.Show("序号【" + 序号 + "】合同号【" + 合同号 + "】的卷重量不符合数字格式，请检查");
                                return;
                            }
                            string 产地 = table.Rows[i]["产地"].ToString();
                            string 交接单号 = table.Rows[i]["交接单号"].ToString();
                            string 入库单号 = table.Rows[i]["入库单号"].ToString();
                            //string 时间 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            string 入库日期 =  table.Rows[i]["入库日期"].ToString();
                            //string 原料卷号 = table.Rows[i]["批号"].ToString();
                            string 原批号 = table.Rows[i]["原批号"].ToString();
                            string 材质 = table.Rows[i]["材质"].ToString();
                            string 入库库位 = table.Rows[i]["库位"].ToString();
                            string 备注 = table.Rows[i]["备注"].ToString();
                            //F11
                            string 入库仓库 = table.Rows[i]["入库仓库"].ToString();
                            //匹配存货编号
                            DataTable tb = pub.直接查询招采平台原料卷存货(品名, 规格型号, 材质);
                            if (tb != null && tb.Rows.Count > 1)
                            {
                                MessageBox.Show("批号【"+ 原批号 + "】匹配出来的存货编号不唯一，请进入存货编号查询界面检查");
                                return;
                            }
                            else if (tb != null && tb.Rows.Count == 1)
                            {
                                存货编号 = tb.Rows[0]["T_GOOD_CODE"].ToString();
                                存货名称 = tb.Rows[0]["T_GOOD_NAME"].ToString();
                            }
                            else
                            {
                                MessageBox.Show("批号【" + 原批号 + "】未能匹配到存货编号，请进入存货编号查询界面检查");
                                return;
                            }
                           

                            string 厚度;
                            string 宽度;
                            string 虚拟值;
                            try
                            {
                                string[] gg = 规格型号.Split("*");

                                厚度 = gg[0];
                                宽度 = gg[1];
                                虚拟值 = gg[2];
                            }
                            catch
                            {
                                MessageBox.Show("规格不符合格式!", "提示", MessageBoxButtons.OK);
                                return;
                            }



                            #region 检查


                            if (原批号 == "")
                            {
                                MessageBox.Show("原料卷号未填写!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (卷重量 <= 0)
                            {
                                MessageBox.Show("卷重量有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (入库仓库 == "")
                            {
                                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (入库库位 == "")
                            {
                                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (材质 == "")
                            {
                                MessageBox.Show("材质有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            if (产地 == "")
                            {
                                MessageBox.Show("产地有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            ///检查原料卷号是否存在了
                            if (pub.IsExistVolume(原批号))
                            {
                                MessageBox.Show("原料卷号["+原批号+"]已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;

                            }

                            if (IsExistTempVolume(原批号))
                            {
                                MessageBox.Show("原料卷号这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            ///检查原料卷号是否存在了
                            if (新卷号 != "")
                            {
                                if (pub.IsExistVolumeNew(新卷号))
                                {
                                    MessageBox.Show("新卷号【"+ 新卷号 + "】已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                                    pub.Delete原料入库临时数据();
                                    return;

                                }

                            }
                            if (!pub.CheckSize(规格型号))
                            {
                                MessageBox.Show("规格型号的表示是不正确的,请正确填写!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;
                            }
                            #endregion

                            ///塞入临时表并显示
                            sql = "insert into Temp_原料卷入库 (guid, 序号, 合同号,  存货编号, 存货名称, 规格型号,  原料卷号,新卷号,入库仓库,入库库位,产地,材质, 卷重量,MAC,时间,T_GOOD_NUM,T_GOOD_PRICE,T_GOOD_MONEY,CID ,Thick, Width, Material,Virtual  )";
                            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "','" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "','" + 原批号 + "', '" + 新卷号 + "','" + 入库仓库 + "','" + 入库库位 + "','" + 产地 + "','" + 材质 + "', '" + 卷重量 + "',";
                            sql += "'" + pub.getMac() + "','"+ 入库日期 + "','0','0','0','','" + 厚度 + "','" + 宽度 + "','" + 材质 + "','C' )";
                            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入原料入库临时表") > 0)
                            {


                            }
                            else
                            {
                                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                                pub.Delete原料入库临时数据();
                                return;

                            }
                        }
                        ListTemp_原料卷入库(tguid, this.uiDataGridView无合同原料卷);
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
                pub.Delete原料入库临时数据();
                return;
            }
        }

        private void uiDataGridView无合同原料卷_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (this.uiDataGridView无合同原料卷.Columns[e.ColumnIndex].Name == "E删除")
                {
                    string sql;
                    int 序号 = pub.ToInt(this.uiDataGridView无合同原料卷.Rows[e.RowIndex].Cells["E序号"].Value.ToString());
                    sql = " delete Temp_原料卷入库 where (guid='" + tguid + "' and 序号='" + 序号 + "') or (mac ='" + pub.getMac() + "' and guid<>'" + tguid + "')";
                    if (pub.ExecuteSql(pub.ConnectionString, sql, "原料入库临时表删除行") >= 0)
                    {
                        //更新序号
                        sql = "select guid, 序号   FROM      Temp_原料卷入库 where guid='" + tguid + "'  order by 序号 ";
                        DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                        if (tb != null)
                        {
                            for (int i = 0; i < tb.Rows.Count; i++)
                            {
                                sql = "update Temp_原料卷入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "更新原料入库临时表的行号");
                            }
                        }
                        ListTemp_原料卷入库(tguid, this.uiDataGridView无合同原料卷);
                    }
                    else
                    {
                        MessageBox.Show("删除出错了,请检查");
                        return;
                    }

                }
            }

        }

        private void uiButton保存2_Click(object sender, EventArgs e)
        {
            ///将临时表塞入入库单
            ///
            string sql;
            bool t = false;
            ///先验证是否重复了
            sql = "select guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位, 原料卷号,新卷号, 卷重量    FROM Temp_原料卷入库 where guid='" + tguid + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (pub.IsExistVolume(tb.Rows[i]["原料卷号"].ToString()))
                    {

                        MessageBox.Show("要保存的卷【" + tb.Rows[i]["原料卷号"].ToString() + "】已经有保存过了!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            string stockInType = "原料卷入库";

            string workShopCode = pub.全局工厂编号;
            string docCode = pub.GetDocCode("原料入库", workShopCode);
            if (this.uiTextBox部门2.Text == "")
            {
                MessageBox.Show("部门未选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            string departCode = pub.GetDepartByCode(this.uiTextBox部门2.Text, 工厂编号1).DepartCode;
            string guid = Guid.NewGuid().ToString();
            //string warehouseCode = this.uiComboBox仓库编号1.SelectedValue.ToString();//仓库编号
            //string storageLocation = this.uiComboBox入库库位1.SelectedValue.ToString();//库位编号
            // string StorageLocation = this.uiComboBox入库库位1.SelectedValue.ToString();
            ///塞入原料主表
            if (docCode == "")
            {
                MessageBox.Show("未获得单号,请联系系统管理员!", "提示", MessageBoxButtons.OK);
                return;
            }

            sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','" + stockInType + "','" + workShopCode + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
            sql += @" insert into Bus_StockInVolume(guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, T_CONTRACT_CODE, 
                T_ORDER, T_GOOD_UNIT, T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, VolumeCode, VolumeCodeNew,InventoryCode, Size, 
                Thick, Width, Material,Virtual, Weight, CID ,Address
)";
            sql += " select  '" + guid + "',newid(),'" + docCode + "',  序号,'" + workShopCode + "', 入库仓库,入库库位,合同号,合同行号,计量单位,0, 0, 0,原料卷号,新卷号,存货编号,规格型号,Thick, Width, Material,Virtual,卷重量,CID,产地 ";
            sql += " from  Temp_原料卷入库 where guid='" + tguid + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "保存原料入库单据");
            ///检查是否成功
            sql = "select * from Bus_StockInV where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sql);
            if (tbr.Rows.Count > 0)
            {
                sql = "select * from Bus_StockInVolume where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tbb.Rows.Count > 0)
                {
                    MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);
                    sql = "delete  Temp_原料卷入库 where guid='" + tguid + "' or mac ='" + pub.getMac() + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "保存成功后删除原料入库临时表");
                    tguid = "";
                    ClearAll1();

                    return;
                }
                else
                {
                    sql = "delete  Bus_StockInV  where guid='" + guid + "'";

                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
            else
            {
                sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return;
            }
        }

        private void uiTextBox厚度2_TextChanged(object sender, EventArgs e)
        {
            SetSize2();
        }

        private void SetSize2()
        {
            this.uiTextBox规格型号2.Text = pub.Todecimal(this.uiTextBox厚度2.Text, 3) + "*" + pub.ToInt(this.uiTextBox宽度2.Text) + "*" + this.uiTextBox虚拟值2.Text;
        }

        private void uiTextBox宽度2_TextChanged(object sender, EventArgs e)
        {
            SetSize2();
        }

        private void uiTextBox虚拟值2_TextChanged(object sender, EventArgs e)
        {
            SetSize2();
        }
    }
}
